package org.xg.study.boot.common.logs;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;

/**
 * @CLassName CodeBookLogger
 * @description
 * @author cmg
 * @date 2021/7/28 10:24
 * @version 1.0
 **/
public class CodeBookLogger implements Serializable {
    private Logger log;
    private CodeBook cb;

    public CodeBookLogger(Class<?> clazz) {
        // initial logger and code book
        log = LogManager.getLogger(clazz);
        cb = CodeBook.I();
    }

    public String debug(int code, String def_val, Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.debug(tmp);
        return tmp;
    }

    public String debug(Throwable ex, int code, String def_val,
                        Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.debug(tmp, ex);
        return tmp;
    }

    public String warn(int code, String def_val, Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.warn(tmp);
        return tmp;
    }

    public String warn(Throwable ex, int code, String def_val, Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.warn(tmp, ex);
        return tmp;
    }

    public String info(int code, String def_val, Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.info(tmp);
        return tmp;
    }

    public String info(Throwable ex, int code, String def_val, Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.info(tmp, ex);
        return tmp;
    }

    public String error(int code, String def_val, Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.error(tmp);
        return tmp;
    }

    public String error(Throwable ex, int code, String def_val,
                        Object... args) {
        String tmp = cb.translate(code, def_val, args);
        log.error(tmp, ex);
        return tmp;
    }
}
